/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2023. All rights reserved.
 */

package com.huawei.aiservice.entity.vo;

import com.huawei.aiservice.constants.ResponseCode;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 响应实体类
 *
 * @author developer
 * @since 2023-1027
 */
@Data
@NoArgsConstructor
public class Response<T> implements Serializable {
    private static final long serialVersionUID = -5892543329889693740L;

    private String code;

    private String message;

    private T data;

    public Response(String code, String message) {
        this(code, message, null);
    }

    public Response(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 通过枚举类 ResponseCode 生成响应体
     *
     * @param responseCode 响应码
     * @return {@link Response}
     */
    public static Response result(ResponseCode responseCode) {
        return new Response(responseCode.getCode(), responseCode.getMessage());
    }

    /**
     * 通过枚举类 responseCode和响应数据 生成响应体
     *
     * @param responseCode 响应码
     * @param data data
     * @return {@link Response}
     */
    public static Response result(ResponseCode responseCode, Object data) {
        return new Response(responseCode.getCode(), responseCode.getMessage(), data);
    }

    /**
     * 通过枚举类 responseCode和响应数据 生成响应体
     *
     * @param responseCode 响应码
     * @param message 响应信息
     * @param data data
     * @return {@link Response}
     */
    public static Response result(ResponseCode responseCode, String message, Object data) {
        return new Response(responseCode.getCode(), message, data);
    }

    /**
     * 通过响应码和响应数据 生成响应体
     *
     * @param code 响应码
     * @param message 响应信息
     * @param data data
     * @return {@link Response}
     */
    public static Response result(String code, String message, Object data) {
        return new Response(code, message, data);
    }

    /**
     * 通过响应码和响应数据 生成响应体
     *
     * @param code 响应码
     * @param message 响应信息
     * @return {@link Response}
     */
    public static Response result(String code, String message) {
        return new Response(code, message);
    }

    /**
     * 通过响应数据 生成响应体
     *
     * @param data data
     * @return {@link Response}
     */
    public static <T >Response<T> success(T data) {
        return new Response<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
    }

    /**
     * 通过响应数据 生成响应体
     *
     * @param data data
     * @return {@link Response}
     */
    public static <T> Response<T> failed(T data) {
        return new Response<T>(ResponseCode.FAILED.getCode(), ResponseCode.FAILED.getMessage(), data);
    }

    /**
     * 通过枚举类 responseCode和响应数据 生成响应体
     *
     * @param responseCode 响应码
     * @param message 响应信息
     * @return {@link Response}
     */
    public static <T> Response<T> failed(ResponseCode responseCode, String message) {
        return new Response<T>(responseCode.getCode(), message);
    }

    /**
     * 通过枚举类 httpStatus和响应数据 生成响应体
     *
     * @param httpStatus httpStatus
     * @param data data
     * @return {@link Response}
     */
    public static Response result(HttpStatus httpStatus, Object data) {
        return new Response(String.valueOf(httpStatus.value()), httpStatus.getReasonPhrase(), data);
    }

    /**
     * 通过枚举类 httpStatus 生成响应体
     *
     * @param httpStatus httpStatus
     * @return {@link Response}
     */
    public static Response result(HttpStatus httpStatus) {
        return new Response(String.valueOf(httpStatus.value()), httpStatus.getReasonPhrase(), null);
    }

    /**
     * 通过枚举类 httpStatus 生成响应体
     *
     * @param httpStatus httpStatus
     * @param message 响应信息
     * @return {@link Response}
     */
    public static Response failed(HttpStatus httpStatus, String message) {
        return new Response(String.valueOf(httpStatus.value()), message, null);
    }
}
